package com.ruomeng.robot.schedule;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import com.ruomeng.robot.service.GoodsUpdateService;
import com.ruomeng.robot.util.SmsTool;
import com.xiaoleilu.hutool.date.DateUtil;
 
/**
 * 定时任务
 * @author Administrator
 *
 */
@Configuration
@EnableScheduling
public class SchedulingConfig {
	
	
	
	int groupUpdateCount=0;
	int goodsUpdateCount=0;
	int couponUpdateCount=0;
	int sendSmsCountToday=0;
	String[] phones= {"17682329352","17682329352"};
	
	@Autowired
    private GoodsUpdateService goodsUpdateService;
   
	/**
	 * 检测群跟新软件
	 */
    @Scheduled(cron = "0 0/2 * * * ? ") // 每20秒执行一次   
    public void scheduler1() {
    	
    	
    	int hour = Integer.parseInt(DateUtil.format(new Date(), "HH"));
    	System.out.println("现在是："+hour+"点");
    	if (hour<=8 || hour>=23) {//在9点到23点之间更新
			return;
		}
    	
    	
    	if (groupUpdateCount>5) {
			SmsTool.noteSend("群更新软件异常了！！！！", phones[1]);
			sendSmsCountToday++;
			groupUpdateCount=0;
			
			return ;
		}
    	
        Map<String,Object> map  = new HashMap<String,Object>();
		String fiveMinAgo =DateUtil.offsetMinute(new Date(), -5).toString("yyyy-MM-dd HH:mm");
		map.put("currentTime", fiveMinAgo);
	    int count =  goodsUpdateService.checkGroupGoodsStatus(map);
	    System.out.println(count);
	    if (count<1) {
	    	groupUpdateCount+=1;
		}
    }
    /**
     * 商品更新
     */
    @Scheduled(cron = "0 0/2 * * * ? ") // 每20秒执行一次   
    public void scheduler2() {
    	
    	
    	int hour = Integer.parseInt(DateUtil.format(new Date(), "HH"));
    	System.out.println("现在是："+hour+"点");
    	if (hour<=8 || hour>=23) {//在9点到23点之间更新
			return;
		}
    	
    	
    	if (goodsUpdateCount>5) {
			SmsTool.noteSend("群更新软件异常了！！！！", phones[1]);
			sendSmsCountToday++;
			goodsUpdateCount=0;
			return ;
		}
    	
        Map<String,Object> map  = new HashMap<String,Object>();
		String fiveMinAgo =DateUtil.offsetMinute(new Date(), -5).toString("yyyy-MM-dd HH:mm");
		map.put("currentTime", fiveMinAgo);
	    int count =  goodsUpdateService.checkGoodsUpdateStatus(map);
	    System.out.println(count);
	    if (count<1) {
	    	goodsUpdateCount+=1;
		}
    }
    /**
     * 券更新
     */
    @Scheduled(cron = "0 0/2 * * * ? ") // 每300秒执行一次   
    public void scheduler3() {
    	
    	
    	int hour = Integer.parseInt(DateUtil.format(new Date(), "HH"));
    	System.out.println("现在是："+hour+"点");
    	if (hour<=8 || hour>=23) {//在9点到23点之间更新
			return;
		}
    	
    	
    	if (couponUpdateCount>5) {
			SmsTool.noteSend("群更新软件异常了！！！！",phones[1]);
			couponUpdateCount=0;
			sendSmsCountToday++;
			return ;
		}
    	
        Map<String,Object> map  = new HashMap<String,Object>();
		String fiveMinAgo =DateUtil.offsetMinute(new Date(), -5).toString("yyyy-MM-dd HH:mm");
		map.put("currentTime", fiveMinAgo);
	    int count =  goodsUpdateService.checkCounponUpdateStatus(map);
	    System.out.println(count);
	    if (count<1) {
	    	couponUpdateCount+=1;
		}
    }
    
    
    
    
    
}